package com.shanzmoo.admin.web.sys;

import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.sys.entity.RoleEntity;
import com.shanzmoo.db.sys.entity.dto.RoleQueryDto;
import com.shanzmoo.db.sys.entity.dto.RoleSaveDto;
import com.shanzmoo.db.sys.entity.dto.RoleUpdateDto;
import com.shanzmoo.db.sys.entity.vo.RolePageVo;
import com.shanzmoo.db.sys.entity.vo.RoleSelectVo;
import com.shanzmoo.db.sys.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author Gzhao
 * @since 2020-11-10
 */
@Api(tags = "系统-角色管理")
@RestController
@RequestMapping("/sys/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "添加")
    @PostMapping("/add")
    public SHttpResponse<Boolean> add(@Valid @RequestBody RoleSaveDto saveDto) {
        RoleEntity roleEntity = RoleSaveDto.converted(saveDto);
        roleEntity.setCreateUser(CurrentUserCache.getUserId());

        SResponse<Boolean> re = roleService.add(roleEntity, saveDto.getMenuIds());
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "删除")
    @GetMapping("/delete")
    public SHttpResponse<Boolean> deleteById(@RequestParam(value = "roleId") Integer roleId) {
        RoleEntity roleEntity = new RoleEntity();
        roleEntity.setDeleted(true);
        roleEntity.setUpdateUser(CurrentUserCache.getUserId());


        SResponse<Boolean> re = roleService.deleteById(roleEntity);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public SHttpResponse<Boolean> update(@Valid @RequestBody RoleUpdateDto updateDto) {
        SResponse<Boolean> re = roleService.edit(updateDto);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "分页列表")
    @PostMapping("/pageList")
    public SHttpResponse<PageRespBase<RolePageVo>> pageList(@Valid @RequestBody RoleQueryDto queryDto) {
        SResponse<PageRespBase<RolePageVo>> re = roleService.getPageList(queryDto);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "获取用户角色id")
    @GetMapping("/getMenuIds/{roleId}")
    public SHttpResponse<List<Integer>> getMenuIdsRoleId(@PathVariable Integer roleId) {
        SResponse<List<Integer>> re = roleService.getMenuIdsRoleId(roleId);
        return SHttpResponse.transform(re);
    }

    @ApiOperation(value = "角色选择列表")
    @GetMapping("/selectList")
    public SHttpResponse<List<RoleSelectVo>> selectList() {
        List<RoleSelectVo> re = roleService.selectList(CurrentUserCache.getOutletId());
        return SHttpResponse.ok(re);
    }
}
